<?PHP
error_reporting(0);

//some globals
$backgroundDir = "./images/background_image/";
$avatarDir = "./images/avatars/";
$avatarExtension = ".gif";
$classImageDir = "./images/class_image/";
$classImageExtension = ".gif";
$raceImageDir = "./images/race_image/";
$raceImageExtension = ".gif";
$professionImageDir = "./images/profession_image/";
$professionImageExtension = ".gif";
$talentImageDir = "./images/talent_image/";
$talentImageExtension = ".png";
$fontFile = "./DejaVuSans.ttf";
$fallbackImage = 'sig000';
$watermark = './images/watermark.png';
$xmlCacheDir = "./xml_cache/";
$xmlCacheTime = 3600 * 5; //seconds
$languageDir = './lang/';

// prepare md5 function for sqlite
function sqlite_md5($string)
{
	return md5($string);
}
// include classes
Include("inc/getCharData.inc.php");
Include("inc/signatureTool.inc.php");

// read database
$db = new SQLite3("db/db.sdb");
$db->createFunction('sqlite_md5', 'sqlite_md5');
$id = $db->escapeString($_GET['id']);
$result = $db->query("SELECT * FROM character WHERE sqlite_md5(id)='$id' limit 1");
while ($row = $result->fetchArray())
{
	$textField[0] = $row['value1'];
	$textField[1] = $row['value2'];
	$textField[2] = $row['value3'];
	$textField[3] = $row['value4'];
	$textField[4] = $row['value5'];

	if($row['language'])
	$language = $row['language'];
	else
	$language = "de";

	$zone = $row['pool'];
	$char_name = $row['name'];
	$char_realm = $row['realm'];
	$backgroundImage = $row['background'];
	$hexColor = "#".$row['color'];
	if( $row['text_outline'] == 1 )
	{
		$textOutline = true;
	}
	else
	{
		$textOutline = false;
	}
}
// update timestamp
$db->query("UPDATE character SET 'last_used' = '".time()."' WHERE sqlite_md5(id)='$id'");
$db->close();

if($language == "")
{
	$language = "de";
}
$ImagePath = $backgroundDir.$backgroundImage;
if(empty($ImagePath))
{
	$background = $fallbackImage;
}
else
{
	$background = $backgroundImage;
}


function error_image($message)
{
	global $backgroundDir;
	global $background;
	if(file_exists($backgroundDir.$background.'.png'))
	{
		$image = ImageCreateFromPNG($backgroundDir.$background.'.png');
	}
	else
	{
		$image = ImageCreateFromPNG($backgroundDir.$fallbackImage.'.png');
	}
	$color = ImageColorAllocate($image, 255, 255, 255);
	imagestring($image,12,10,10,$message,$color);
	header('Content-type: image/png');
	imagepng($image);
	imagedestroy($image);
	exit;
}

$signature = new signatureTool();

if(file_exists($backgroundDir.$background.'.png'))
{
	$signature->createFromBackground($backgroundDir.$background.'.png');
}
else
{
	$signature->createFromBackground($backgroundDir.$fallbackImage.'.png');
}

switch ($zone)
{
	case("RG"):
		$arsenal = 'http://arsenal.rising-gods.de/';
		$xmlFile = $arsenal.'character-sheet.xml?n='.$char_name.'&r='.$char_realm;
		$break;
}

if(!File_exists($languageDir.$language.'.lang.inc.php'))
{
	error_image("Sprache nicht gefunden:".$language);
}
else
{
	include($languageDir.$language.'.lang.inc.php');
}

$charData = new getCharData();
$charData->readXml($xmlFile);

if(!$charData->xmlInput)
{
	error_image("Character nicht gefunden:".$char_name);
}

// Avatar + Border
$signature->insertImage(4,32,$charData->getAvatarUrl(),'gif',true,$hexColor);

// Class Image + Border
$signature->insertImage(73,78,$charData->getClassImageUrl(),'gif',true,$hexColor);

// Race Image + Border
$signature->insertImage(73,55,$charData->getRaceImageUrl(),'gif',true,$hexColor);

// Level + Border
$signature->insertTextBox(73,32,90,49,10,$hexColor,'#E4CA42','#000000',$fontFile,$charData->data('level'));

//Profession
if($charData->data('level')>4)
{
	//1st Profession
	$signature->insertImage(2,2,$charData->getProfessionImageUrl(0),'gif',false);
	$signature->insertText(29,9,9,0,$hexColor,$fontFile,$charData->profession(0,'value'),$textOutline);

	//2nd Profession
	$signature->insertImage(62,2,$charData->getProfessionImageUrl(1),'gif',false);
	$signature->insertText(89,9,9,0,$hexColor,$fontFile,$charData->profession(1,'value'),$textOutline);
}

// Char Name
$signature->insertText(102,31,16,0,$hexColor,$fontFile,$charData->data('name'),$textOutline);

// Guilde and Realm Name
$signature->insertText(102,55,8,0,$hexColor,$fontFile,$charData->data('guildName'),$textOutline);
$x=$signature->lastTextSize[4]+102;
if($charData->data('guildName') != "")
{
	$signature->drawLine($x+9,54,$x+9,65,$hexColor);
}
else
{
	$x = $x - 18;
}
$signature->insertText($x+18,55,8,0,$hexColor,$fontFile,($zone."-".$charData->data('realm')),$textOutline);

// Line
$signature->drawLine(100,70,340,70,$hexColor);

//Talents Main Spec
if($charData->data('level')>9)
{
	$signature->insertImage(102,75,$charData->getTalentImageUrl(1),'png',false);
	$text = $lang[utf8_encode($charData->talent('prim'))].
					" - ".$charData->talent('treeOne').
					"/".$charData->talent('treeTwo').
					"/".$charData->talent('treeThree');
	$signature->insertText(127,81,9,0,$hexColor,$fontFile,$text,$textOutline);
}
// Right Border Text
// replace strings in $textField[*]
for($i=0;$i<5;$i++)
{
	switch($textField[$i])
	{
		case 'health':
			$textField[$i] = $charData->data($textField[$i]). " ".$lang['hp'];
			break;
		case 'mana':
			$textField[$i] = $charData->data($textField[$i]);
			switch($charData->data('classId'))
			{ // no mana for rogues and wariors ;_;
				default:
					$textField[$i] .= " ".$lang['mana'];
					break;
				case '1':
					$textField[$i] .= " ".$lang['rage'];
					break;
				case '4':
					$textField[$i] .= " ".$lang['energy'];
					break;
			}
			break;
		case '2on2_rating':
			$textField[$i] = "2vs.2 Rating: ".$charData->data($textField[$i]);
			break;
		case '3on3_rating':
			$textField[$i] = "3vs.3 Rating: ".$charData->data($textField[$i]);
			break;
		case '5on5_rating':
			$textField[$i] = "5vs.5 Rating: ".$charData->data($textField[$i]);
			break;
		case 'strength':
			$textField[$i] = $charData->data($textField[$i]). " ".$lang['strength'];
			break;
		case 'agility':
			$textField[$i] = $charData->data($textField[$i]). " ".$lang['agility'];
			break;
		case 'stamina':
			$textField[$i] = $charData->data($textField[$i])." ".$lang['stamina'];
			break;
		case 'intellect':
			$textField[$i] = $charData->data($textField[$i])." ".$lang['intellect'];
			break;
		case 'spirit':
			$textField[$i] = $charData->data($textField[$i]). " ".$lang['spirit'];
			break;
		case 'meleeAp':
			$textField[$i] = $charData->data($textField[$i]). " ".$lang['meleeAp'];
			break;
		case 'meleeHit':
			$textField[$i] = $charData->data($textField[$i]). " ".$lang['meleeHit'];
			break;
		case 'meleeArp':
			$textField[$i] = $charData->data($textField[$i]). " ".$lang['meleeArp'];
			break;
		case 'meleeCrit':
			$textField[$i] = $charData->data($textField[$i]). " ".$lang['meleeCrit'];
			break;
		case 'expertise':
			$textField[$i] = $charData->data($textField[$i]). " ".$lang['expertise'];
			break;
		case 'rangedAp':
			$textField[$i] = $charData->data($textField[$i]). " ".$lang['rangedAp'];
			break;
		case 'rangedHit':
			$textField[$i] = $charData->data($textField[$i]). " ".$lang['rangedHit'];
			break;
		case 'rangedArp':
			$textField[$i] = $charData->data($textField[$i]). " ".$lang['rangedArp'];
			break;
		case 'rangedCrit':
			$textField[$i] = $charData->data($textField[$i]). " ".$lang['rangedCrit'];
			break;
		case 'spellPower':
			$textField[$i] = $charData->data($textField[$i]). " ".$lang['spellPower'];
			break;
		case 'healingPower':
			$textField[$i] = $charData->data($textField[$i]). " ".$lang['healingPower'];
			break;
		case 'spellHit':
			$textField[$i] = $charData->data($textField[$i]). " ".$lang['spellHit'];
			break;
		case 'spellReduceResist':
			$textField[$i] = $charData->data($textField[$i]). " ".$lang['spellReduceResist'];
			break;
		case 'spellCrit':
			$textField[$i] = $charData->data($textField[$i]). " ".$lang['spellCrit'];
			break;
		case 'haste':
			$textField[$i] = $charData->data($textField[$i]). " ".$lang['haste'];
			break;
		case 'armor':
			$textField[$i] = $charData->data($textField[$i]). " ".$lang['armor'];
			break;
		case 'defense':
			$textField[$i] = $charData->data($textField[$i]). " ".$lang['defense'];
			break;
		case 'avoid':
			$textField[$i] = $charData->data($textField[$i]). " ".$lang['avoid'];
			break;
		case 'dodge':
			$textField[$i] = $charData->data($textField[$i]). " ".$lang['dodge'];
			break;
		case 'parry':
			$textField[$i] = $charData->data($textField[$i]). " ".$lang['parry'];
			break;
		case 'block':
			$textField[$i] = $charData->data($textField[$i]). " ".$lang['block'];
			break;
		case 'resilience':
			$textField[$i] = $charData->data($textField[$i]). " ".$lang['resilience'];
			break;
	}
}

// now print the text
$fontSize = 9;
for($i=0;$i<5;$i++)
{
	$textField[$i] = $textField[$i];
	$y = 10+$fontSize*$i+($fontSize/1.7)*$i;
	$signature->insertText(10,$y,$fontSize,0,$hexColor,$fontFile,$textField[$i],$textOutline,'right');
}

// Watermark
$signature->insertImage(390,83,$watermark,'png',false);

// Generate Image
$signature->createImage();

?>